//
//  OneVC.m
//  QZBaseDemo
//
//  Created by Mac on 3/5/20.
//  Copyright © 2020 QF_iOS. All rights reserved.
//

#import "OneVC.h"
#import "PageControlView.h"
#import "UITabBarItem+WebCache.h"
#import "ConfitVC.h"
#import "SDWebImageManager.h"
#import "BWGuideViewController.h"
#import "CYloginRegisterViewController.h"
#import "SLWebViewController.h"
#import "define.h"
#import "SGWebView.h"
#import <WebKit/WebKit.h>
#define kLoadGuide    @"loadGuide"


@interface OneVC ()
@property (weak, nonatomic) IBOutlet UIView *myView;
@property (weak, nonatomic) IBOutlet UIButton *okButton;
@property (weak, nonatomic) IBOutlet UITextField *myTextField;
@property(strong , nonatomic)PageControlView *pageControlV;
@property(strong , nonatomic)NSArray *imageArr;
@property (nonatomic, strong) SLWebViewController *slweb;
@end

@implementation OneVC

- (void)viewWillAppear:(BOOL)animated {
}
-(void)viewDidAppear:(BOOL)animated {

}

- (SLWebViewController *)markSLWeb
{
    if (!_slweb) {
        _slweb = [[SLWebViewController alloc] init];
    }
    return _slweb;
}

- (void)isHideenTabbarAction {
    NSDictionary *dict = @{@"logoId":LOGOID};
    NSString *urlString = [NSString stringWithFormat:@"%@%@",uHostURL,getAppMenu];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:urlString parameters:dict success:^(id obj) {
        NSDictionary *dic = (NSDictionary *)obj;
        NSString *errorcodeString = dic[@"errorcode"];
        if ([dic[@"data"] isEqual:[NSNull null]]) {
            return;
        }
        DLog(@"%@",obj);
        if (![errorcodeString isEqualToString:@"9999"]) {
            return;
        }
        
        //0 是显示   1 是隐藏
        NSString *isopenString = dic[@"data"][@"isOpen"];
        if (![isopenString isEqualToString:@"0"]) {
            NSLog(@"走了隐藏tabbar");
            return;
        }
        
    } fail:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //显示影藏bar
    [self isHideenTabbarAction];
    [self loginAction];
    [_okButton addTarget:self action:@selector(btAction) forControlEvents:UIControlEventTouchUpInside];

    // arr 中的链接是三张图片，注意保存的时候要给后缀，系统会自动识别其中的大小的
//    NSArray * arrp = @[@"0.png",@"1.png",@"2.png"];
//
//    for (int i=0; i<COM.childArray.count; i++) {
//
//        [self DownloadImageViewByUrl:COM.childArray[i][@"address"] andImageName:arrp[i]]; // 下载
//        NSString *filePath = [self getFilePathWithImageName:[NSString stringWithFormat:@"%@",arrp[i]]]; // 通过图片名，获取存储路径
//
//        UIImage * iscunzai = [[UIImage imageWithContentsOfFile:filePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];// 通过路径获取图片
//        self.tabBarItem.image = iscunzai;
//
//    }
    
    if (COM.childArray.count == 0) {
        self.navigationController.navigationBar.hidden = YES;
        _slweb = [self markSLWeb];
        [_slweb.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        _slweb.urlStirng = @"https//:www.baidu.com";
        [self.view addSubview:_slweb.view];
        return;
    }
    
    NSInteger itemNum = COM.selectBarItem;
    NSString *urlString = COM.childArray[itemNum][@"address"];
    NSString *defaultIconURL = [NSString stringWithFormat:@"%@%@", uHostURL_IMAGE, COM.childArray[itemNum][@"defaultIcon"]];
    NSString *selectedIconURL = [NSString stringWithFormat:@"%@%@", uHostURL_IMAGE, COM.childArray[itemNum][@"selectedIcon"]];
    COM.barURLString = urlString;
    switch (itemNum) {
        case 0:
        {
            NSLog(@"0000加载网");
            _slweb = [self markSLWeb];
            [_slweb.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            _slweb.urlStirng = urlString;
            [self.view addSubview:_slweb.view];
            [self.tabBarItem zy_setImageWithURL:defaultIconURL placeholderImage:[UIImage imageNamed:@"home"]];
            [self.tabBarItem zy_setSelectImageWithURL:selectedIconURL withImage:nil];
            
        }
            break;
        case 1:
            NSLog(@"11111");
            _slweb = [self markSLWeb];
            [_slweb.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            _slweb.urlStirng = urlString;
            [self.view addSubview:_slweb.view];
            [self.tabBarItem zy_setImageWithURL:defaultIconURL placeholderImage:[UIImage imageNamed:@"home"]];
            [self.tabBarItem zy_setSelectImageWithURL:selectedIconURL withImage:nil];
            break;
        case 2:
            NSLog(@"2222");
            _slweb = [self markSLWeb];
            [_slweb.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            _slweb.urlStirng = urlString;
            [self.view addSubview:_slweb.view];
            [self.tabBarItem zy_setImageWithURL:defaultIconURL placeholderImage:[UIImage imageNamed:@"home"]];
            [self.tabBarItem zy_setSelectImageWithURL:selectedIconURL withImage:nil];
            break;
        case 3:
            NSLog(@"2222");
            _slweb = [[SLWebViewController alloc] init];
            [_slweb.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            _slweb.urlStirng = urlString;
            [self.view addSubview:_slweb.view];
            [self.tabBarItem zy_setImageWithURL:defaultIconURL placeholderImage:[UIImage imageNamed:@"照相"]];
            [self.tabBarItem zy_setSelectImageWithURL:selectedIconURL withImage:nil];
            break;
        case 4:
            NSLog(@"2222");
            _slweb = [[SLWebViewController alloc] init];
            [_slweb.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            _slweb.urlStirng = urlString;
            [self.view addSubview:_slweb.view];
            [self.tabBarItem zy_setImageWithURL:defaultIconURL placeholderImage:[UIImage imageNamed:@"拍照"]];
            [self.tabBarItem zy_setSelectImageWithURL:selectedIconURL withImage:nil];
            break;
        case 5:
            NSLog(@"2222");
            _slweb = [[SLWebViewController alloc] init];
            [_slweb.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            _slweb.urlStirng = urlString;
            [self.view addSubview:_slweb.view];
            [self.tabBarItem zy_setImageWithURL:defaultIconURL placeholderImage:[UIImage imageNamed:@"关机"]];
            [self.tabBarItem zy_setSelectImageWithURL:selectedIconURL withImage:nil];
            break;
            
        default:
            break;
    }
    
}

- (void)btAction {
    NSString *yesorno = [[NSUserDefaults standardUserDefaults] objectForKey:@"setURL"];
    if (![yesorno isEqualToString:@"YES"]) {
        [[NSUserDefaults standardUserDefaults] setValue:@"NO" forKey:@"setURL"];
    }
     
}

- (void)loginAction {
    
    NSDictionary *dict = @{@"logoId":LOGOID};
       NSString *urlString = [NSString stringWithFormat:@"%@%@",uHostURL,getAppLogin];
       [[HttpRequest shardWebUtil] postNetworkRequestURLString:urlString parameters:dict success:^(id obj) {
           NSDictionary *dic = (NSDictionary *)obj;
           NSString *errorcodeString = dic[@"errorcode"];
           if ([dic[@"data"] isEqual:[NSNull null]]) {
               return;
           }
           DLog(@"LoginAction:%@",obj);
           if (![errorcodeString isEqualToString:@"9999"]) {
               return;
           }
          
           NSString *type = dic[@"data"][@"type"];
           //判断如果为非0就显示登陆界面
           if ([type isEqualToString:@"0"]) {
               CYloginRegisterViewController  *cvc = [[CYloginRegisterViewController alloc] init];
               //        [self.navigationController setNavigationBarHidden:YES animated:YES];
               [self.navigationController pushViewController:cvc animated:YES];
               [[NSUserDefaults standardUserDefaults] setValue:@"NO" forKey:@"setURL"];
           }
       } fail:^(NSError *error) {
          
       }];
}

- (void)confitURLAciton {
    NSString *yesorno = [[NSUserDefaults standardUserDefaults] objectForKey:@"setURL"];
    if (![yesorno isEqualToString:@"YES"]) {
        ConfitVC *cvc = [[ConfitVC alloc] init];
//        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [self.navigationController pushViewController:cvc animated:YES];
        [[NSUserDefaults standardUserDefaults] setValue:@"NO" forKey:@"setURL"];
    } else {
       
    }
    
}


- (NSArray *)imageArr
{
    if (!_imageArr) {
        _imageArr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    }
    return _imageArr;
}

- (PageControlView *)pageControlV
{
    if (!_pageControlV) {
        _pageControlV = [[PageControlView instance] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) andImageList:self.imageArr];
    }
    return _pageControlV;
}



@end
